package com.yolo.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yolo.common.annotation.Log;
import com.yolo.common.enums.BusinessType;
import com.yolo.entity.Notice;
import com.yolo.entity.dto.NoticeDto;
import com.yolo.service.NoticeService;
import com.yolo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 天蝎
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/notice")
@CrossOrigin
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 查询并分页
     *
     * @return
     */
    @GetMapping("")
    @Log(title = "通知模块", businessType = BusinessType.QUERY)
    public Result noticeAll(NoticeDto noticeDto) {
        QueryWrapper<Notice> qw = new QueryWrapper<>();
        if (noticeDto.getNoticeTitle() != null && !noticeDto.getNoticeTitle().equals("")) {
            qw.like("notice_title", noticeDto.getNoticeTitle());
        }
        if (noticeDto.getNoticeType() != null && !noticeDto.getNoticeType().equals("")) {
            qw.eq("notice_type", noticeDto.getNoticeType());
        }
        IPage<Notice> noticeIPage = new Page<>(noticeDto.getPageNo(), noticeDto.getPageSize());
        IPage<Notice> page = noticeService.page(noticeIPage, qw);
        return Result.succ(MapUtil.builder()
                .put("count", page.getTotal())
                .put("data", page.getRecords())
                .map()
        );
    }


    /**
     * 添加通知
     */
    @Log(title = "通知模块", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result addNotice(Notice notice) {
        noticeService.save(notice);
        return Result.succ(null);
    }

    /**
     * 删除通知
     */
    @Log(title = "通知模块", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result deleteNotice(@PathVariable("id") int noticeId) {
        noticeService.removeById(noticeId);
        return Result.succ(null);
    }

    /**
     * 根据编号查询数据
     *
     * @param noticeId
     * @return
     */
    @Log(title = "通知模块", businessType = BusinessType.QUERY)
    @GetMapping("/{id}")
    public Result noticeById(@PathVariable("id") int noticeId) {
        Notice notice = noticeService.getById(noticeId);
        return Result.succ(notice);
    }

    @Log(title = "通知模块", businessType = BusinessType.DELETE)
    @DeleteMapping("/batchDelete/{ids}")
    public Result delete(@PathVariable("ids") String ids) {
        if (ids != null && !ids.equals("")) {
            String[] split = ids.split(",");
            List<Integer> noticeIds = new ArrayList<>();
            for (String s : split) {
                noticeIds.add(Integer.parseInt(s));
            }
            noticeService.removeByIds(noticeIds);
        }
        return Result.succ(null);
    }

    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadImg")
    public String upload(@RequestParam("image") MultipartFile file) throws IOException {
        String path = ResourceUtils.getURL("classpath:").getPath() + "static/";
        File descDir = new File(path);
        if (!descDir.exists()) {
            descDir.mkdirs();
        }
        String fileName = file.getOriginalFilename();
        file.transferTo(new File(path + fileName));
        return "http://localhost:8888/" + fileName;
    }

    @Log(title = "通知模块",businessType = BusinessType.QUERY)
    @PostMapping("/noticeType")
    public Result noticeType() {
        List<Notice> tz = noticeService.list(new QueryWrapper<Notice>().eq("notice_type", 0));
        List<Notice> gg = noticeService.list(new QueryWrapper<Notice>().eq("notice_type", 1));
        return Result.succ(MapUtil.builder()
                .put("tz", tz)
                .put("gg", gg)
                .map());
    }

}
